package cn.eeepay.lib_core.ktx

import android.view.View
import androidx.annotation.IdRes
import androidx.annotation.LayoutRes
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.ConvertUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.zzq.my.jetpacks.adapter.RecyclerViewDivider
import com.zzq.my.jetpacks.base.R

/*
  * ================================================
  * 描述：基于作者的BaseQuickAdapter 抽象出共性的adapter，从此可以避免写adapter的烦恼了
  * 作者：zhuangzeqin
  * 时间: 2023/11/1 9:27
  * 邮箱：zzq@eeepay.cn
  * 备注:
  * ----------------------------------------------------------------
  * You never know what you can do until you try !
  *      _              _           _     _   ____  _             _ _
  *     / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
  *    / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
  *   / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
  *  /_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/
  *
  * 签名：最痛苦的事不是我失败了，而是我本可以.--zzq
  * ----------------------------------------------------------------
  * ================================================
  * eg:
  *     val list = listOf<String>("abc1","abc2","abc3","abc4","abc5","abc6")
        dataBinding.rvList.linear(可选).divider(可选).set<String>(R.layout.agent_home_dialog_policy_pop_list_item) { holder, data ->
            holder.setText(R.id.tvTransactionType,data)
        }.setItemClick { adapter, view, position ->
            adapter.getItem(position).toString().show()
        }.setItemChildClickView(R.id.tvTransactionType) { adapter, view, position ->
            adapter.getItem(position).toString().show()
        }.setList(list)
  */
class CommonBaseAdapter<T> constructor(
    @LayoutRes private val layoutResId: Int,
) : BaseQuickAdapter<T, BaseViewHolder>(layoutResId) {
    private var bindDataFun: ((BaseViewHolder, T) -> Unit)? = null

    /**
     * 绑定数据
     */
    fun bindData(block: ((BaseViewHolder, T) -> Unit)) {
        bindDataFun = block
    }

    /**
     * Implement this method and use the helper to adapt the view to the given item.
     *
     * 实现此方法，并使用 helper 完成 item 视图的操作
     *
     * @param holder A fully initialized helper.
     * @param item   The item that needs to be displayed.
     */
    override fun convert(holder: BaseViewHolder, item: T) {
        bindDataFun?.invoke(holder, item)
        val i =10
        if (i in 1..10)
        {

        }
    }
}

/**
 * 扩展函数添加一个RecyclerView下划线
 */
fun RecyclerView.divider(dpValue: Float = 0.5F, colorId: Int = R.color.lib_core_EEEEEE, dividerMarginLeft: Float = 0F, dividerMarginRight: Float = 0f): RecyclerView {
    this.addItemDecoration(
        RecyclerViewDivider(
            context,
            LinearLayoutManager.HORIZONTAL,
            ConvertUtils.dp2px(dpValue),
           ContextCompat.getColor(context,colorId), ConvertUtils.dp2px(dividerMarginLeft), ConvertUtils.dp2px(dividerMarginRight)
        )
    )
    return this
}

/**
 * 设置垂直或者横向布局
 */
fun RecyclerView.linear(
    @RecyclerView.Orientation orientation: Int = RecyclerView.VERTICAL
): RecyclerView {
    val manager = LinearLayoutManager(context)
    manager.orientation = orientation
    layoutManager = manager
    return this
}

/**
 * 设置适配器
 */
fun <T> RecyclerView.set(@LayoutRes layoutResId: Int, block: ((BaseViewHolder, T) -> Unit)): CommonBaseAdapter<T> {
    val adapter = CommonBaseAdapter<T>(layoutResId)
    adapter.bindData(block)
    this.adapter = adapter
    return adapter
}

/**
 * 设置item 点击事件
 */
fun <T> CommonBaseAdapter<T>.setItemClick(block: (adapter: BaseQuickAdapter<*, *>, view: View, position: Int) -> Unit): CommonBaseAdapter<T> {
    this.setOnItemClickListener { adapterInfo, v, pos ->
        block.invoke(adapterInfo, v, pos)
    }
    return this
}

/**
 * 设置item 内控件的 点击事件
 */
fun <T> CommonBaseAdapter<T>.setItemChildClickView(@IdRes vararg viewIds: Int, block: (adapter: BaseQuickAdapter<*, *>, view: View, position: Int) -> Unit): CommonBaseAdapter<T> {
    this.getChildClickViewIds().addAll(viewIds.toList())
    this.setOnItemChildClickListener { adapterInfo, v, pos ->
        block.invoke(adapterInfo, v, pos)
    }
    return this
}

/**
 * 设置item 长按点击事件
 */
fun <T> CommonBaseAdapter<T>.setItemLongClick(block: (adapter: BaseQuickAdapter<*, *>, view: View, position: Int) -> Boolean): CommonBaseAdapter<T> {
    this.setOnItemLongClickListener { adapterInfo, v, pos ->
        block.invoke(adapterInfo, v, pos)
    }
    return this
}

/**
 * 设置item 内控件的 长按点击事件
 */
fun <T> CommonBaseAdapter<T>.setItemChildLongClickView(@IdRes vararg viewIds: Int, block: (adapter: BaseQuickAdapter<*, *>, view: View, position: Int) -> Boolean): CommonBaseAdapter<T> {
    this.getChildLongClickViewIds().addAll(viewIds.toList())
    this.setOnItemChildLongClickListener { adapterInfo, v, pos ->
        block.invoke(adapterInfo, v, pos)
    }
    return this
}